<?php

namespace mpend\modules\team\models;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * WorkbookBalanceLogic
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class WorkbookBalanceLogic extends \common\logics\WorkbookLogic {

    /**
     * 获取记工记录
     * @return \common\db\ActiveQuery Description
     */
    public function getUnbalanceRecords() {
        $query = $this->hasMany(WorkbookBalanceRecord::class, ['workbook_id' => 'id'])->active(WorkbookBalanceRecord::STATUS_ACTIVE);
        $query->addSelect([
            "*",
            'balanceAmount' => "SUM(`balance`*`amount`)"
        ]);
        $query->with(['member']);
        $query->groupBy('member_id');
        $query->having(['>', 'balanceAmount', 0]);
        return $query->all();
    }

    public function getBalanceStats() {
        return [
            'title' => '未结算（元）',
            'description' => '未结算=总工钱-借支金额-结算金额',
            'color' => '#F5A623',
            'value' => $this->getUncleared(),
        ];
    }

    public function fields() {
        return [
            'id',
            'title',
            'thumb',
            'description',
            'headman',
            'balanceStats',
            'records' => 'unbalanceRecords',
            'canManage',
        ];
    }

}
